package com.ch1;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.BitSet;

/**
 * 采用位图 文件太大 需要分几次读入
 * 由于读取时间太长 没有全部测试 但是测一次是可以的
 * @author Linxiaozhi
 *
 */
public class BitSort11 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if (args.length < 1) {
			System.out.println("usage: java -Xms2M -Xmx2M com.ch1.BitSort1 d:\\input.txt d:\\output.txt");
			return;
		}

		BitSort11 client = new BitSort11();

		long l1 = System.currentTimeMillis();

		client.fun(args[0], args[1]);

		long l2 = System.currentTimeMillis();
		System.out.println("time:" + (l2 - l1));

	}

	static int TIMES = 256;

	void fun(String inputFilePath, String outputFilePath) {

		try {
			PrintWriter pw = new PrintWriter(outputFilePath);
			BitSet bs = new BitSet(10 ^ 7);
			String s = "";

			int unit = 1000000000 / TIMES;

			for (int counter = 0; counter < TIMES; counter++) {
				int low = counter * unit;
				int high = low + unit;
				BufferedReader br = new BufferedReader(new FileReader(inputFilePath));
				try {
					while ((s = br.readLine()) != null) {
						int in = Integer.parseInt(s);
						if (in >= low && in < high){
							bs.set(in - low);
						}
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
				br.close();
				int size = bs.size();
				for (int i = 0; i < size; i++) {
					if (bs.get(i)) {
						pw.println(i + low);
					}
				}
				pw.flush();
				if(counter == 2) break;
			}
			pw.close();
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
